package com.wzy.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.common.utils.PageUtils;
import com.wzy.common.utils.Query;
import com.wzy.gulimall.product.dao.CategoryDao;
import com.wzy.gulimall.product.entity.CategoryEntity;
import com.wzy.gulimall.product.service.CategoryBrandRelationService;
import com.wzy.gulimall.product.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> getCategoryTree() {
        //查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //处处所有一级分类, 父级分类id = 0
        List<CategoryEntity> levelMenus = categoryEntities.stream().filter((item) -> {
            return item.getParentCid() == 0;
        }).map((menu) -> {
            menu.setChildren(getChildren(menu, categoryEntities));
            return menu;
        }).sorted(Comparator.comparing(CategoryEntity::getSort))
                .collect(Collectors.toList());
        return levelMenus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //todo 分类批量删除待办
        baseMapper.deleteBatchIds(asList);

    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        ArrayList<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);

    }

    @Resource
    CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 级联更新所有的数据
     *
     * @param category
     */
    @Override
    @Transactional
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());

    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildren(CategoryEntity currentEntity, List<CategoryEntity> all) {

        List<CategoryEntity> children = all.stream().filter((item) -> {
            return item.getParentCid() == currentEntity.getCatId();
        }).map(entity -> {
            entity.setChildren(getChildren(entity, all));
            return entity;
        }).sorted(Comparator.comparing(CategoryEntity::getCatId)).collect(Collectors.toList());
        return children;
    }

}